package com.groupten.ServiceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.groupten.Dao.TeacherDao;
import com.groupten.Entity.StudentEntity;
import com.groupten.Entity.TeacherEntity;
import com.groupten.Service.TeacherService;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import com.groupten.Utility.PasswordUtils;
@Service("teacherService")
public class TeacherServiceImpl extends ServiceImpl<TeacherDao, TeacherEntity> implements TeacherService {
    private final TeacherDao teacherDao;

    public TeacherServiceImpl(TeacherDao teacherDao) {
        this.teacherDao = teacherDao;
    }

    @Override
    public Boolean insertTeacher(TeacherEntity teacherEntity) {
        String encryptedPassword = PasswordUtils.encrypt(teacherEntity.getTeacherPassword());
        teacherEntity.setTeacherPassword(encryptedPassword);
        int insert = teacherDao.insert(teacherEntity);
        return insert > 0;
    }
    @Override
    public Integer deleteByName(String TeacherName) {
        LambdaQueryWrapper<TeacherEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ObjectUtils.isNotEmpty(TeacherName), TeacherEntity::getTeacherName, TeacherName);

        return baseMapper.delete(queryWrapper);
    }

    @Override
    public Integer deleteByNumber(String TeacherNumber) {
        LambdaQueryWrapper<TeacherEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ObjectUtils.isNotEmpty(TeacherNumber), TeacherEntity::getTeacherName, TeacherNumber);

        return baseMapper.delete(queryWrapper);
    }

    @Override
    public IPage<TeacherEntity> selectAll(Integer page, Integer limit) {

        int curPage = 1;
        int curLimit = 10;
        if (ObjectUtils.isNotEmpty(page)) {
            curPage = Integer.parseInt(page.toString());
        }
        if (ObjectUtils.isNotEmpty(limit)) {
            curLimit = Integer.parseInt(limit.toString());
        }

        IPage<TeacherEntity> pageObject = Page.of(curPage, curLimit);

        return baseMapper.selectPage(pageObject, Wrappers.lambdaQuery());
    }

    @Override
    public TeacherEntity selectByNumber(String TeacherNumber) {
        LambdaQueryWrapper<TeacherEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ObjectUtils.isNotEmpty(TeacherNumber), TeacherEntity::getTeacherNumber, TeacherNumber);

        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public TeacherEntity selectByName(String TeacherName) {
        LambdaQueryWrapper<TeacherEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ObjectUtils.isNotEmpty(TeacherName), TeacherEntity::getTeacherName, TeacherName);

        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public TeacherEntity selectByPassword(String Password) {
        LambdaQueryWrapper<TeacherEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ObjectUtils.isNotEmpty(Password), TeacherEntity::getTeacherPassword, Password);

        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public TeacherEntity selectById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public Boolean updateById(Long id, Map<String, String> newData) {
        TeacherEntity teacherEntity = selectById(id);
        for (Map.Entry<String, String> entry : newData.entrySet()) {
            switch (entry.getKey()){
                case "teacherName":
                    teacherEntity.setTeacherName(entry.getValue());
                    break;
                case "teacherNumber":
                    teacherEntity.setTeacherNumber(entry.getValue());
                    break;
                case "teacherGender":
                    teacherEntity.setTeacherGender(entry.getValue());
                case "teacherEmail":
                    teacherEntity.setTeacherEmail(entry.getValue());
                    break;
                case "teacherPassword":
                    teacherEntity.setTeacherPassword(PasswordUtils.encrypt(entry.getValue()));
                    break;
                default:
                    break;
            }
        }
        return baseMapper.updateById(teacherEntity)>0;
    }

    @Override
    public List<TeacherEntity> selectAll() {
        return baseMapper.selectList(Wrappers.lambdaQuery());
    }
}
